package com.yj.ccs.abs.web.asset.mapper;

import com.yj.ccs.abs.web.asset.entity.Remark;
import com.yj.ccs.abs.web.asset.entity.RemarkListItem;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/22
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface RemarkMapper {

    @Options(useGeneratedKeys = true, keyProperty = "item.id")
    @Insert("INSERT INTO `remark` (asset_id, date, person, type, fee_date, fee_money, detail, create_time) " +
            "VALUES (#{item.asset_id}, #{item.date}, #{item.person}, #{item.type}, #{item.fee_date}, " +
            "#{item.fee_money}, #{item.detail}, NOW())")
    void create(@Param("item") Remark item);

    @Update("UPDATE `remark` SET type = #{item.type}, fee_date = #{item.fee_date}, fee_money = #{item.fee_money}, detail = #{item.detail} " +
            "WHERE id = #{item.id}")
    void update(@Param("item") Remark item);

    @Delete("DELETE FROM `remark` WHERE id = #{id}")
    void deleteById(@Param("id") Long id);

    @Select("SELECT * FROM `remark` WHERE id = #{id} LIMIT 1")
    Remark getById(@Param("id") Long id);

    @Select("SELECT id, date, person, type, fee_money, detail FROM `remark` WHERE asset_id = #{asset_id}")
    List<RemarkListItem> getListItemByAssetId(@Param("asset_id") Long asset_id);

    @Select("SELECT SUM(fee_money) FROM remark WHERE asset_id = #{asset_id}")
    BigDecimal getTotalMoneyByAssetId(@Param("asset_id") Long asset_id);
}
